package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import modle.Country;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.awt.Color;
import java.awt.Toolkit;

public class View_Orders extends JFrame {

	private JPanel contentPane;
	private JButton btnShip;
	private JButton btnSearch;
	private JButton btnBack;
	private JComboBox comboBox;
	private List<Country> listCountries;
	private Locale locale;
	private JLabel omLabel;
	ResourceBundle rd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					View_Orders frame = new View_Orders();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public View_Orders() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(View_Orders.class.getResource("/img/iconImage.jpg")));
		setTitle("Welcome to Food Corner");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 570, 470);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(View_Orders.class.getResource("/img/orders1.jpg")));
		lblNewLabel.setBounds(53, 102, 154, 136);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setIcon(new ImageIcon(View_Orders.class.getResource("/img/orders2.jpg")));
		lblNewLabel_1.setBounds(342, 102, 154, 136);
		contentPane.add(lblNewLabel_1);
		
		btnShip = new JButton("Ship");
		btnShip.setFont(new Font("微软雅黑", Font.BOLD, 19));
		btnShip.setBounds(65, 297, 119, 35);
		contentPane.add(btnShip);
		btnShip.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Ship_Orders().setVisible(true);
				dispose();
			}
		});
		
		btnSearch = new JButton("Search");
		btnSearch.setFont(new Font("微软雅黑", Font.BOLD, 19));
		btnSearch.setBounds(351, 297, 119, 35);
		contentPane.add(btnSearch);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Search_Orders().setVisible(true);
				dispose();
			}
		});
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("微软雅黑", Font.PLAIN, 19));
		btnBack.setBounds(14, 376, 84, 35);
		contentPane.add(btnBack);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Admin_Menu().setVisible(true);
				dispose();
			}
		});
		
		omLabel = new JLabel("Order Management");
		omLabel.setForeground(new Color(165, 42, 42));
		omLabel.setFont(new Font("微软雅黑", Font.PLAIN, 21));
		omLabel.setBounds(159, 34, 220, 47);
		contentPane.add(omLabel);
		
		comboBox = new JComboBox<>();						//组合按钮或可编辑字段和下拉列表的组件
		comboBox.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 17));
		comboBox.addItemListener(new ItemListener() {		//用于接收项目事件的侦听器接口。
			public void itemStateChanged(ItemEvent e) {
				int index = comboBox.getSelectedIndex();	//返回列表中与给定项目匹配的第一个项目。 
					String arr[] = listCountries.get(index).getId().split("-");					//  ???（应该是遇见 - 就分割）
					locale = new Locale(arr[0], arr[1]);//	???   Locale对象代表具体的地理，政治或文化地区。（第一个是语言，第二个是国家）
					rd = ResourceBundle.getBundle("resources.content", locale);		//使用指定的基本名称，区域设置和类加载器获取资源包（这里好像没用类加载器）。 
					try {
						changeLanguage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
			}
		});
		comboBox.setBounds(440, 13, 98, 32);
		contentPane.add(comboBox);
		
		fillCountries();
	}
	
	private void fillCountries() {
		// TODO Auto-generated method stub
		listCountries = new ArrayList<Country>();
		listCountries.add(new Country("en-US", "English"));
		listCountries.add(new Country("zh-CN", "中文"));
		
		for(Country c : listCountries)
			comboBox.addItem(c.getName());
	}

	protected void changeLanguage() {
		// TODO Auto-generated method stub
		
		omLabel.setText(rd.getString("omLabel"));
		btnSearch.setText(rd.getString("btnSearch"));
		btnShip.setText(rd.getString("btnShip"));
		btnBack.setText(rd.getString("btnBack"));
	}

}
